import util from '@/utils/util'
import filter from '@/utils/filter'
import {mapState} from 'pinia'
import {mainStore, userStore} from '@/store'

export default {
  data () {
    return {
			filter: filter as object,
			meta: {title:'',color:'',keywords:'',desc:''} as object,
			querys: {} as object,
			loadstate: 0 as number,
			tipShow: false,
			loadPath: '' as string,
			tipData: [],
			needLogin: false as boolean
    }
  },
	computed: {
		...mapState(userStore, ['auth']),
		...mapState(mainStore, ['idtype', 'location'])
	},
	onPullDownRefresh() {
		if(!this.loadPath){
			uni.stopPullDownRefresh()
			return
		}
		this.page && (this.page = 1)
		this.load().then(() => {
			uni.stopPullDownRefresh()
		})
	},
	onLoad(querys){
		this.meta.title && uni.setNavigationBarTitle({title: this.meta.title})
		this.meta.color && uni.setNavigationBarColor({
			frontColor: '#ffffff',
			backgroundColor: this.meta.color
		})
		Object.assign(this.querys, querys)
	},
	onReady (){
		this.loadPath && this.load(this.loadPath, this.querys)		
	},
	methods: {
		load (path: string = '', querys: object) {
			this.loadstate = 1
			return uni.$u.http.get(path || this.loadPath, {params: querys}).then((data: any)=>{
				this.loadData(data)
				this.loadstate = 2
			}).catch(e=>uni.hideLoading())
		},
		reload(){
			this.loadstate = 1
			return uni.$u.http.get(this.loadPath + this.$u.queryParams({...this.querys, page: this.page})).then((data: any)=>{
				this.loadData(data)
				this.loadstate = 2
			}).catch(()=>{})
		},
		frush(){
			this.loadstate = 1
			return uni.$u.http.get(this.loadPath, this.querys).then(data=>{
				this.loadData(data)
				this.loadstate = 2
			}).catch(()=>{})
		},
		loadData (data) {
			Object.assign(this, data)
		},
		copy(text){
			uni.setClipboardData({
				data: text,
				success () {
					uni.showToast({title:'复制成功'})
				},
				fail(){
					
				}
			})
		},
		rdrto (path, params = {}) {
			util.rdrto(path, params)
		},
		navto (path, params = {}) {
			util.navto(path, params)
		},
		back (path, params = {}) {
			util.back(path, params)
		},
		preview (url, i = 0) {
			util.preview(url, i)
		},
		toast (text) {
			util.toast(text)
		},
		tip (tips) {
			this.tipShow = true
			this.tipData = tips
		}
	}
}
